import cv2
from .color import get_colors
from .tracking import track_objects
from .frame_processor import process_frame
from .image import resize_image

def process_image(file_path, seg_model, emotion_model):
    try:
        image = cv2.imread(file_path)
        if image is None:
            raise ValueError("无法读取图片文件")

        from deep_sort_realtime.deepsort_tracker import DeepSort
        tracker = DeepSort(max_age=5, n_init=2)
        colors = get_colors(10)
        
        # 获取检测结果和处理后的帧
        detections, processed_frame = process_frame(image, seg_model, emotion_model)
        print(f"从process_frame获取的检测结果: {detections}")  # 调试信息
        
        # 创建情绪字典
        id_emotions = {}
        
        # 首先尝试直接使用检测结果
        if detections:
            print("使用检测结果")  # 调试信息
            for i, detection in enumerate(detections):
                emotion_label = detection[2]  # 获取情绪标签
                print(f"检测到的原始情绪标签: {emotion_label}")  # 调试信息
                # 将情绪标签转换为中文
                emotion_chinese = {
                    'Happy': '高兴',
                    'Sad': '伤心',
                    'Angry': '生气',
                    'Surprise': '惊讶',
                    'Neutral': '中性',
                    'Calm': '平静',
                    'Fear': '恐惧',
                    'Disgust': '厌恶',
                    'Contempt': '蔑视',
                    'Unknown': '未知'
                }.get(emotion_label, '未知')
                id_emotions[i+1] = emotion_chinese
                print(f"转换后的情绪: {emotion_chinese}")  # 调试信息
        else:
            # 如果没有检测到人脸
            print("未检测到任何人脸")  # 调试信息
            id_emotions[0] = '未检测到人脸'
            
        print(f"最终返回的情绪结果: {id_emotions}")  # 调试信息
        return processed_frame, id_emotions
    except Exception as e:
        print(f"处理图片时出错: {e}")
        return None, {'error': str(e)}